import { Controller, Get, Query } from '@nestjs/common';
import { MatchServiceV2 } from './match.service.v2';
import { ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { MatchInfo } from './entities/match.entity';
import { Group } from '../group/entities/group.entity';
import { Stage } from '../stage/entities/stage.entity';
import { Category } from '../category/entities/category.entity';
import { Playground } from '../playground/entities/playground.entity';
import { Tournament } from '../tournament/entities/tournament.entity';
import { GameInfo } from '../game/entities/game.entity';
import { Event } from '../event/entities/event.entity';
import { EventToPlayer } from '../event-to-player/entities/event-to-player.entity';
import { Auth } from 'src/common/decorators/auth.decorator';

type MatchDetailType = Partial<{
  match: Partial<MatchInfo>;
  group: Partial<Group>;
  stage: Partial<Stage>;
  event: Partial<Event>;
  category: Partial<Category>;
  tournament: Partial<Tournament>;
  playground: Partial<Playground>;
  games: Partial<GameInfo>[];
  homePlayerInfo: Partial<EventToPlayer>;
  awayPlayerInfo: Partial<EventToPlayer>;
}>;

@Auth()
@ApiBearerAuth()
@Controller('match')
export class MatchControllerV2 {
  constructor(private readonly matchService: MatchServiceV2) {}

  @Get('getMatchDetailV2')
  @ApiOperation({
    summary: '获取比赛详情',
    description: '根据比赛ID获取比赛的详细信息',
  })
  @ApiQuery({
    name: 'matchId',
    type: 'string',
    description: '比赛ID',
  })
  async getMatchDetail(
    @Query('matchId') matchId: string | number,
  ): Promise<MatchDetailType> {
    const retMatchData = await this.matchService.getMatchDetail(
      parseInt(matchId + ''),
    );
    const { playground, games, ...matchRest } = retMatchData;
    return {
      match: matchRest,
      playground,
      games,
      homePlayerInfo: retMatchData.homePlayer,
      awayPlayerInfo: retMatchData.awayPlayer,
    };
  }

  @Get('getInProgressMatchesV2')
  @ApiOperation({
    summary: '获取进行中的比赛',
    description: '根据赛事ID获取所有正在进行中的比赛',
  })
  @ApiQuery({
    name: 'tournamentId',
    type: 'string',
    description: '赛事ID',
  })
  async getInProgressMatches(
    @Query('tournamentId') tournamentId: string | number,
  ): Promise<Array<MatchInfo>> {
    return await this.matchService.getInProgressMatches(
      parseInt(tournamentId + ''),
    );
  }
}
